package com.coolwen.spring.aop.java;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author CoolWen
 * @version 2020-03-26 8:48
 */
@Component
@Aspect
public class PerformAspectUpdate {

    @Pointcut("execution(* com.coolwen.spring.aop.java.CalFibonacciImpl.*(..))")
    public void performPonintcut() {

    }

    //使用before的advise
    @Before("performPonintcut()")
    public void testBefore() {
        System.out.println("run before");
    }

    //使用after的advise
    @After("performPonintcut()")
    public void testAfter() {
        System.out.println("run after");
    }

    //效率统计，使用环绕
    @Around("performPonintcut()")
    public long statPerform(ProceedingJoinPoint jp) {
        long result = 0;
        try {
            long start = System.currentTimeMillis();
            result = (long) jp.proceed();
            long end = System.currentTimeMillis();
            System.out.println(jp.getSignature().getName() + "花费了:" + (end - start));
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return result;
    }

    @Pointcut("execution(* com.coolwen.spring.aop.java.CalFibonacciImpl.calFibonacciByLoop(..))&&args(arg)")
    public void testArg(long arg) {
    }

    @Before("testArg(arg)")
    public void testBefore(long arg) {
        System.out.println("基于参数的方法" + arg);
    }
}
